package com.rq.adapter.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rq.adapter.LOG;
import com.rq.adapter.R;
import com.rq.rvlibrary.AdapterHolderController;
import com.rq.rvlibrary.AdapterUseException;
import com.rq.rvlibrary.BaseAdapter;
import com.rq.rvlibrary.BaseViewHolder;
import com.rq.rvlibrary.EasyViewHolder;
import com.rq.rvlibrary.EmptyViewHolder;
import com.rq.rvlibrary.NestingBuilder;
import com.rq.rvlibrary.OnClickMaker;
import com.rq.rvlibrary.OnDataBinding;
import com.rq.rvlibrary.OnPressMaker;
import com.rq.rvlibrary.ViewDataGetter;

import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.List;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RankAdapter extends RecyclerView.Adapter {
    List showData;
    Context mContext;
    int resId;

    public RankAdapter(Context rankRVAct, int item_green) {
        this.mContext = rankRVAct;
        this.resId = item_green;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        viewHolder = getViewHolderByClass(EasyViewHolder.class, this.resId, parent, viewType);
        return viewHolder;
    }
    /**
     * 通过反射获取Holder实例
     */
    protected RecyclerView.ViewHolder getViewHolderByClass(@NonNull Class<?> holderRoot, @LayoutRes int resId,
                                      ViewGroup parent, int viewType) {
        String error = "";
        String holdName = "";
        if (holderRoot == null) {
            holderRoot = EasyViewHolder.class;
        }
        holdName = holderRoot.getSimpleName();
        if (EmptyViewHolder.class.getSimpleName().equals(holdName)) {
            View itemView = LayoutInflater.from(mContext).inflate(resId, parent, false);
            itemView.setTag(com.rq.rvlibrary.R.id.rv_child_layout, resId);
            return (RecyclerView.ViewHolder) new EmptyViewHolder(itemView);
        }

        try {
            Constructor<?>[] ctors = holderRoot.getDeclaredConstructors();
            if (ctors != null && ctors.length > 0) {
                View itemView = LayoutInflater.from(mContext).inflate(resId, parent, false);
                RecyclerView.ViewHolder holder=null;
                try {
                    holder = (RecyclerView.ViewHolder) ctors[0].newInstance(itemView);
                } catch (IllegalArgumentException e) {
                   e.printStackTrace();
                }
                if (holder == null) {
                    throw new AdapterUseException(holderRoot
                            .getSimpleName() + " 获取到了一个空  Holder -_-||  --> " + viewType);
                }
                holder.itemView.setTag(com.rq.rvlibrary.R.id.rv_child_layout, resId);

                return holder;
            }
        } catch (InstantiationException e) {
            error = "Exception:InstantiationException";
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            error = "Exception:IllegalAccessException";
            e.printStackTrace();
        } catch (Exception e) {
            error = "Exception:e";
            e.printStackTrace();
        }
        if (error != null && error.contains("Wrong number of arguments")) {
            error = error + "\n【【【" + holderRoot
                    .getSimpleName() + ".调用类内部类ViewHolder 调用四参数构造方法 或者 重写 getMore() 内容 】】】";
        }
        String errName = holderRoot == null ? "ERROR" : holderRoot.getName();
        throw new AdapterUseException(errName + " 初始化异常:" + error);
    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        LOG.e("BaseAdapter", "545:onViewRecycled");
        if (holder instanceof ViewDataGetter) {
            LOG.e("BaseAdapter", "onViewRecycled:" + ((ViewDataGetter) holder).getViewData());
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        onBindEasyHolder(null, (EasyViewHolder) holder, position, showData.get(position));
    }

    protected void onBindEasyHolder(BaseAdapter adapter, BaseViewHolder holder, int position, Object o) {

    }

    @Override
    public int getItemCount() {
        return showData != null ? showData.size() : 0;
    }

    public void setData(List<String> d) {
        this.showData = d;
        notifyDataSetChanged();
    }

    public void moveItem(int fromPosition, int toPosition) {
        Collections.swap(showData, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }
}
